//
//  SCWishInfoController.m
//  Mutal
//
//  Created by TonyStart on 16/10/27.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCWishInfoController.h"
#import "SCWishInfoHeaderView.h"
#import "SCWishInfoCellHeaderView.h"
#import "SCWishCommentViewModel.h"
#import "SCWishCommentCell.h"
#import "SCWishCommentFooterView.h"
@interface SCWishInfoController ()
@property(nonatomic,strong)SCWishInfoHeaderView *wishHeaderView;
@property(nonatomic,strong)SCWishCommentViewModel *wishCommentViewModel;
@property(nonatomic,strong)SCWishCommentFooterView *footerView;
@end
#define FOOTER_HEIGHT 60
@implementation SCWishInfoController

-(SCWishInfoHeaderView *)wishHeaderView{
    if (!_wishHeaderView) {
        _wishHeaderView = [[UINib nibWithNibName:@"SCWishInfoHeader" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
        _wishHeaderView.wish = self.wish;
        _wishHeaderView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 280);
    }
    return _wishHeaderView;
}
-(SCWishCommentViewModel *)wishCommentViewModel{
    if (!_wishCommentViewModel) {
        _wishCommentViewModel = [[SCWishCommentViewModel alloc] init];
    }
    return _wishCommentViewModel;
}
-(SCWishCommentFooterView *)footerView{
    if (!_footerView) {
        _footerView = [[SCWishCommentFooterView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-FOOTER_HEIGHT-64, [UIScreen mainScreen].bounds.size.width, FOOTER_HEIGHT)];
//        _footerView.backgroundColor = [UIColor redColor];
    }
    return _footerView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadWishData];
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableHeaderView = self.wishHeaderView;
    
    
    
}
-(void)viewDidAppear:(BOOL)animated{
    [self.view addSubview:self.footerView];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offsetY = scrollView.contentOffset.y + scrollView.contentInset.top;
    
    self.footerView.frame = CGRectMake(0, self.view.frame.size.height-FOOTER_HEIGHT-64+offsetY, self.view.frame.size.width, FOOTER_HEIGHT);;
    

    
    
    
}

-(void)loadWishData{
    
    [self.wishCommentViewModel requestWishCommentWithPostID:self.wish.wish_id finish:^(NSInteger num) {
        
        [self.tableView reloadData];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.wishCommentViewModel.wishComments.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCWishCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wishInfoCell" forIndexPath:indexPath];
    cell.wishCommentModel = self.wishCommentViewModel.wishComments[indexPath.row];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SCWishInfoCellHeaderView *cellHeaderView = [[UINib nibWithNibName:@"SCWishInfoCellHeader" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
    
 

    cellHeaderView.wish = self.wish;
    
    return cellHeaderView;
    
}


@end
